package com.bilin.coin;

import android.app.Application;
import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bilin.coin.utils.SharedPreferencesUtils;
import com.bumptech.glide.request.target.ViewTarget;
import com.bun.miitmdid.core.JLibrary;
import com.lb.mmv.ADv;
import com.liulishuo.filedownloader.FileDownloader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;
import com.umeng.socialize.UMShareAPI;
import com.yanzhenjie.album.Album;
import com.yanzhenjie.album.AlbumConfig;

import java.util.HashMap;
import java.util.Locale;

import androidx.multidex.MultiDex;
import kt.com.bilin.coin.helper.MiitHelper;
import kt.com.bilin.coin.utils.ActivityManger;
import kt.com.bilin.coin.utils.GlideAlbumLoader;
import kt.com.bilin.coin.utils.Logger;


/**
 * Created by LG on 2018/4/14.
 */

public class App extends Application {
    private static Context mContext;
    public static String OAID;

    public static Context getApp() {
        return mContext;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        try {
            JLibrary.InitEntry(this);
        } catch (Throwable e) {
            Logger.e(e);
        }
        MultiDex.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        if (BuildConfig.DEBUG) {
            ARouter.openLog();
            ARouter.openDebug();
            ARouter.printStackTrace();
        }
        ADv.initApp("blsq_new", this);
        MiitHelper miitHelper = new MiitHelper(oaid -> OAID = oaid);
        miitHelper.getDeviceIds(this);
        SharedPreferencesUtils.init(this);
        ARouter.init(this);
        mContext = getApplicationContext();
//        FusionAdSDK.init(this, "192323392974618624");
        ActivityManger.registerActivityLifecycleCallbacks(this);
        CrashReport.initCrashReport(getApplicationContext(), "8880572068", BuildConfig.DEBUG);
        initUmeng();
        Album.initialize(
                AlbumConfig.newBuilder(this)
                        .setAlbumLoader(new GlideAlbumLoader()) // 设置Album加载器。
                        .setLocale(Locale.CHINA) // 比如强制设置在任何语言下都用中文显示。
                        .build()
        );
        ViewTarget.setTagId(R.id.glide_tag);
        HashMap map = new HashMap();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_DEXLOADER_SERVICE, true);
        QbSdk.initTbsSettings(map);
        QbSdk.initX5Environment(this, null);
        FileDownloader.setup(this);
    }

    private void initUmeng() {
        UMConfigure.setLogEnabled(BuildConfig.DEBUG);
        UMConfigure.init(this, "5f3f66c5b4b08b653e97b627", "umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        UMConfigure.setProcessEvent(true);//支持多进程打点
        UMShareAPI.get(this);
        PlatformConfig.setWeixin(ApiConstant.WX_APP_ID, "a243be72ed204eb46c9c5e9682591bb4");
        PlatformConfig.setAlipay(ApiConstant.ALI_APP_ID);
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
    }

    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.bg_main_gray, R.color.text_color);//全局设置主题颜色
                return new ClassicsHeader(context);//.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.bg_main_gray, R.color.text_color);//全局设置主题颜色
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(20);
            }
        });
    }
}
